<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>书籍充值金额折扣</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
    <style>
        .layui-form-label{ width: 102px; }
        .layui-input-block{ margin-left: 152px;}
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card" style="height: 600px;padding-top: 20px;">
        <div class="layui-form">

            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 154px;">总折扣比例（%）</label>
                <div class="layui-input-block">
                    <input style="width: 200px;float: left;margin-right: 10px;" type="number" name="book_discount_money" value="{$book_discount_money}"  placeholder="请输入折扣比例" autocomplete="off" class="layui-input">
                    <div class="layui-form-mid layui-word-aux">例：70代表折扣比例70%，值为0或100代表不折扣</div>
                </div>
            </div>

            <div>
                {empty name="book_discount_money_list"}
                <div class="layui-form-item">
                    <div class="layui-form-item" style="float: left;clear: none">
                        <label class="layui-form-label">书籍ID：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="book_discount_money_nid[]" value="" placeholder="请输入书籍ID" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item" style="float: left;clear: none;">
                        <label class="layui-form-label" style="width: 113px;">折扣比例（%）：</label>
                        <div class="layui-input-inline" style="width:300px;">
                            <input type="number" name="book_discount_money_rate[]" value="" placeholder="请输入折扣比例" autocomplete="off" class="layui-input">
                        </div>

                        <div class="copy-item" style="float: left;line-height: 38px;">[ + ]</div>
                    </div>
                </div>
                {else /}
                {volist name="book_discount_money_list" key="k" id="v"}
                <div class="layui-form-item">
                    <div class="layui-form-item" style="float: left;clear: none">
                        <label class="layui-form-label">书籍ID：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="book_discount_money_nid[]" value="{$v.nid}" placeholder="请输入书籍ID" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item" style="float: left;clear: none;">
                        <label class="layui-form-label" style="width: 113px;">折扣比例（%）：</label>
                        <div class="layui-input-inline" style="width:300px;">
                            <input type="number" name="book_discount_money_rate[]" value="{$v.rate}" placeholder="请输入折扣比例" autocomplete="off" class="layui-input">
                        </div>

                        {eq name="k" value="1"}
                        <div class="copy-item" style="float: left;line-height: 38px;">[ + ]</div>
                        {else /}
                        <div class="remove-item" style="float: left;line-height: 38px;">[ - ]</div>
                        {/eq}

                    </div>
                </div>
                {/volist}
                {/empty}

            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <button class="layui-btn" lay-submit lay-filter="user_active">提交</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function(){
        var $ = layui.$
            ,form = layui.form;

        $('.copy-item').click(function () {
            var html = '';
            html += '<div class="layui-form-item">';
            html += '<div class="layui-form-item" style="float: left;clear: none">';
            html += '<label class="layui-form-label">书籍ID：</label>';
            html += '<div class="layui-input-inline">';
            html += '<input type="text" name="book_discount_money_nid[]" value="" placeholder="请输入书籍ID" autocomplete="off" class="layui-input">';
            html += '</div></div>';
            html += '<div class="layui-form-item" style="float: left;clear: none;">';
            html += '<label class="layui-form-label" style="width: 113px;">折扣比例（%）：</label>';
            html += '<div class="layui-input-inline" style="width:300px;">';
            html += '<input type="number" name="book_discount_money_rate[]" value="" placeholder="请输入折扣比例" autocomplete="off" class="layui-input">';
            html += '</div><div class="remove-item" style="float: left;line-height: 38px;">[ - ]</div>';
            html += '</div></div>';

            $(this).parent().parent().parent().append(html);

        })

        $('body').on('click','.remove-item',function () {
            $(this).parent().parent().remove();
        })

        form.on('submit(user_active)', function(data){

            if(data.field.book_discount_money < 0)
            {
                layer.msg('请输入不小于0的值');
                return false;
            }
            if(data.field.book_discount_money > 100)
            {
                layer.msg('请输入不大于100的值');
                return false;
            }

            $.ajax({
                url:"{:url('Tool/book_discount_money')}",
                type:"post",
                dataType: "json",
                data:data.field,
                async:true,
                success: function(rs){
                    if(rs['code'] == 100){

                        layer.msg(rs['msg'],function () {
                            layer.close(); //关闭弹层
                        });

                    }else{
                        layer.msg(rs['msg']);
                    }
                    return false;
                }
            });
        });

    })

</script>
</body>
</html>